/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-03-31 14:58:28
 * @LastEditors: cuidm
 * @LastEditTime: 2022-10-08 16:24:56
 */
import React, { useState, useEffect } from "react";
import { Modal, Form, Button, Input, Switch , Select, message, Space } from "antd";
import { HcpIconSelect } from "@/components";
import cmdbres from "@/data/cmdbres";
import request from "@/utils/request";
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const user = hcpUtils.getUser();
const EditModel = ({ visible, setVisible, onEditFinish, info}) => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);

    useEffect(() => {
		if(info && visible) {
            form.setFieldsValue(info);
            form.setFieldsValue({ 
                isRoot: info.isRoot === 1 ? true:false,
            });
        }
	}, [info, visible]);

	const onFinish = async (values) => {
		setLoading(true);
		let res = await request({
			...cmdbres.updateModel,
			data: {
                ...info,
				moduleName: values.moduleName,
				moduleIcon: values.moduleIcon,
				author: user.staffName,
			}
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success("修改模型成功！");
			if (typeof onEditFinish == "function") onEditFinish(res, values.moduleName);
		} else {
			if(res) message.error(res.msg);
		}
	};

	const onCancel = () => {
		setVisible(false);
	}

	return (
		<div>
			<Modal
				title="修改模型"
				className="hcp-modal-sm"
				visible={visible}
				footer={null}
				onCancel={onCancel}
				destroyOnClose
				maskClosable={false}
				afterClose={()=>{form.resetFields()}}
			>
				<Form
					form={form}
					labelCol={{ span: 5 }}
					wrapperCol={{ span: 18 }}
					name="basic"
					onFinish={onFinish}
				>
					<Form.Item 
						label="模型名称" 
						name="moduleName" 
						required
					>
						<Input placeholder="请输入名称" maxLength={64} disabled={info?.moduleClassificationType == "inner"}/>
					</Form.Item>
					<Form.Item 
						label="唯一标识"
						name="moduleCode"
						required
						initialValue={info?.moduleCode}
					  >
						<Input placeholder="请输入唯一标识" maxLength={256} disabled />
					</Form.Item>
					<Form.Item
						name="moduleIcon"
						label="图标"
						rules={[ { required: true, message: '请选择图标' }]}
					>
						<HcpIconSelect placeholder="点击选择图标" />
					</Form.Item>
					<div className="hcp-modal-body-footer">
						<Form.Item wrapperCol={{ span: 24 }} >
							<Button onClick={onCancel}>取消</Button>
							<Button type="primary" htmlType="submit" loading={loading}>确认</Button>
						</Form.Item>
					</div>
				</Form>
			</Modal>
		</div>
	)
}

export default EditModel;